package com.greenant.store.view;

import com.greenant.store.R;
import com.greenant.store.bean.EMRecv;
import com.greenant.store.service.RetrieveDataAsyn;
import com.greenant.store.service.RetrieveDataListener;
import com.greenant.store.utils.UtilTools;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ExpressTabView2 extends RelativeLayout {

	private Context mContext;
	private Button makeSend;
	private EditText sendText;
	private TextView makeSendHint;

	public ExpressTabView2(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		init();
	}

	public ExpressTabView2(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		init();
	}

	public ExpressTabView2(Context context) {
		super(context);
		mContext = context;
		init();
	}

	private void init() {
		View view = LayoutInflater.from(mContext).inflate(R.layout.view_express_tab2, this);

		makeSend = (Button) view.findViewById(R.id.makeSend);
		sendText = (EditText) view.findViewById(R.id.sendText);
		makeSendHint = (TextView) view.findViewById(R.id.makeSendHint);
		makeSend.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (TextUtils.isEmpty(sendText.getText().toString())) {
					makeSendHint.setText(R.string.emrecv_send_company_hint9);
				} else {
					RetrieveDataAsyn.getInstance().updateEmrecvStatus(sendText.getText().toString(),
							EMRecv.EMRECV_TYPE_EXPORT, 0, new RetrieveDataListener() {

						@Override
						public void dataRetrieved(int requestId, Object data) {
							if (data == null) {
								UtilTools.showMyToast(mContext, mContext.getString(R.string.failed));
								makeSendHint.setText(R.string.emrecv_send_company_hint9);
							} else {
								UtilTools.showMyToast(mContext, mContext.getString(R.string.success));
								EMRecv emrecv = (EMRecv) data;

								if(emrecv.getEMCompany()!=null)
								{
									makeSendHint.setText(mContext.getString(R.string.emrecv_send_company_hint10,
											emrecv.getEMCompany().getCompanyName(), emrecv.getEMId()));
								}
								else{
									makeSendHint.setText(mContext.getString(R.string.emrecv_send_company_hint10,
											"", emrecv.getEMId()));
								}
								
								
							}
						}
					});
				}
			}
		});
		
		sendText.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				makeSendHint.setText("");
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				
			}
		});
	}

}
